<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>demo02</title>
</head>
<body>
  <div id="div1"></div>
  <!-- call apply -->
  <script>
    var obj1 = {
      name: 'obj1'
    }

    var obj2 = {
      name: 'obj2'
    }

    var getName = function() {
      return this.name;
    }

    console.log(getName.call(obj1), getName.call(obj2))

    // 修正 this
    document.getElementById = (function(func) {
      return function() {
        return func.apply(document, arguments );
      }
    })(document.getElementById)

    var getId = document.getElementById

    var div = getId('div1')

    console.log(div)

    // 实现 Function.prototype.bind
    Function.prototype.bind = function(context) {
      var __this = this;
      return function() {
        __this.apply(context, arguments);
      }
    }

    var func = function() {
      console.log(this.name)
    }.bind(obj2)

    func()
    
  </script>
</body>
</html>